<%@ page contentType="text/html;charset=UTF-8"%>
<%@ page import="com.ejoysoft.common.Constants,
				com.ejoysoft.util.ParamUtil,
				com.ejoysoft.wlanyz.business.Paper,
				com.ejoysoft.common.*,
				com.ejoysoft.wlanyz.business.Ac,
				java.io.File,
				 java.util.ArrayList,
				 java.util.Vector,
				 java.io.FileNotFoundException "%>
<%@ include file="../include/jsp/head.jsp"%>
<%	
 		
try{
        if(action.equals(Constants.DELETE_IMG)){
        
		
     	Paper obj=new Paper(globa,false);
	    String strUrl="paper_list.jsp";
     	//String[] aryStrId = ParamUtil.getStrArray(request, "strId");
     	String strHotSpotId = ParamUtil.getString(request, "strHotSpotId");
	    String[] aryStrId = ParamUtil.getStrArray(request, "strId");
     	
     	
     	Ac ac = new Ac(globa,false);
     	String where = " where strid ='" + strHotSpotId + "'";
     	ac = ac.show(where);
     	String strCity = ac.getStrCity();
		String strCountry = ac.getStrCountry();
		String classOneAc = ac.getClassOneAC();
		String classTwoAc = ac.getClassTwoAC();
		String strFilePath1 = application.getRealPath("") + "/acpapers/江苏省/" + strCity + "/" + strCountry + "/" + classOneAc +"/" + classTwoAc+"/" ;
		strFilePath1 = strFilePath1.replace('#','_');
     	for (int i = 0; i < aryStrId.length; i++) {
     	
 			Paper obj0 = new Paper(globa,false);
			 where = " where intfloor ='" + aryStrId[i] + "' and acname = '"+strHotSpotId+"'";
			String cadName = obj0.getImgName(where);
			
    		  // Paper obj0 = obj.show(" where strId='" + strHotSpotId[i] + "'");
    			File f = new File(strFilePath1+ cadName);
    			if(!f.isDirectory())
			{f.delete();
			 obj.setImgflag1(cadName,0,strHotSpotId);
			}
    		}
    		globa.dispatch(true, strUrl);  
	}
        else{
        if(action.equals(Constants.DELETE_STR))
        {
        Paper obj=new Paper(globa,false);
	    String strUrl="paper_list.jsp";
     	//String[] aryStrId = ParamUtil.getStrArray(request, "strId");
     	String[] strHotSpotId = ParamUtil.getStrArray(request, "strId");
     	for (int i = 0; i < strHotSpotId.length; i++) {
 			Ac ac = new Ac(globa,false);
			String where = " where strid ='" + strHotSpotId[i] + "'";
			ac = ac.show(where);
			String strCity = ac.getStrCity();
			String strCountry = ac.getStrCountry();
			String classOneAc = ac.getClassOneAC();
			String classTwoAc = ac.getClassTwoAC();
			String strFilePath1 = application.getRealPath("") + "/acpapers/江苏省/" + strCity + "/" + strCountry + "/" + classOneAc +"/" + classTwoAc ;
	 		strFilePath1 = strFilePath1.replace('#','_');
	 		String strFilePath11 = application.getRealPath("") + "/acpapers/江苏省/" + strCity + "/" + strCountry + "/" + classOneAc +"/" + classTwoAc +"1";
	   	     strFilePath11 = strFilePath11.replace('#','_');
	   	     File path11 = new File(strFilePath11);	
	   	    if (!path11.exists()) {
	    	path11.mkdirs();
	        }
	           File f = new File(strFilePath1);
	    		if(f.isDirectory())
    			{
    			String ff[] = f.list();
    			for(int j=0;j<ff.length;j++)
    			{
    			File delfile = new File(strFilePath1 + "\\" + ff[j]); 
    			File move = new File(strFilePath11+ "\\"+ff[j]);//将原来的图纸移动到临时文件夹内
    			delfile.renameTo(move);
    			if (!delfile.isDirectory()) delfile.delete(); 
               else if (delfile.isDirectory()) { 
               delfile.delete();
               
               }
    		}
    		f.delete();
    		}
    		  // Paper obj0 = obj.show(" where strId='" + strHotSpotId[i] + "'");
    			f = new File(strFilePath1 );
    			if(f.isDirectory())
    			{
    			String ff[] = f.list();
    			for(int j=0;j<ff.length;j++)
    			{
    			File delfile = new File(strFilePath1 + "\\" + ff[j]); 
    			if (!delfile.isDirectory()) { 
                System.out.println("path=" + delfile.getPath()); 
                System.out.println("absolutepath=" + delfile.getAbsolutePath()); 
                System.out.println("name=" + delfile.getName()); 
                delfile.delete(); 
                System.out.println("删除文件成功"); 
                } 
               else if (delfile.isDirectory()) { 
               delfile.delete();
               //deletefile(strFilePath + "\\" + ff[i]); 
               }
    		}
    		f.delete();
    		}
        	if(obj.delete("where acname ='"+strHotSpotId[i]+"'"))
        	{
        	f = new File(strFilePath11 );
    			if(f.isDirectory())
    			{
    			String ff[] = f.list();
    			for(int j=0;j<ff.length;j++)
    			{
    			File delfile = new File(strFilePath11 + "\\"+ ff[j]); 
    			if (!delfile.isDirectory()) delfile.delete(); 
                
               else if (delfile.isDirectory()) { 
               delfile.delete();
               }
    		}
    		f.delete();
    		}
    		globa.dispatch(true, strUrl); 	
        	}
        	else 
        	{
        	f = new File(strFilePath11);
	    		if(f.isDirectory())
    			{
    			String ff[] = f.list();
    			for(int j=0;j<ff.length;j++)
    			{
    			File delfile = new File(strFilePath11 +"\\"+ ff[j]); 
    			File move = new File(strFilePath1+ "\\"+ff[j]);//将动到临时文件夹内的图纸移动回原来的文件夹内
    			delfile.renameTo(move);
    			if (!delfile.isDirectory())delfile.delete(); 
                else if (delfile.isDirectory()) { 
               delfile.delete();
               //deletefile(strFilePath + "\\" + ff[i]); 
               }
    		}
    		f.delete();
    		}
    		globa.dispatch(false, strUrl); 
         }	
     	}
   
	}
	else{
	
	   ApacheUpload au = new ApacheUpload(request);
	   action = au.getString(Constants.ACTION_TYPE);
	   System.out.println("dddddddddddddddddd"+action);
	   if(action.equals(Constants.ADD_STR))
	   {
	   String strUrl="paper_import.jsp";
	    String strUrl1="paper_update.jsp";
	    String strUrl2="paper_list.jsp";
		String classoneac = au.getString("strHotSpot");
		String classtwoac = au.getString("classTwoAc");
		int num = Integer.parseInt(au.getString("papernum"));
		String tWhere = " where classoneac = '" +classoneac+"'  and classtwoac = '"+classtwoac+"' ";
		Paper objx = new Paper(globa,false);
		if(objx.isexist(tWhere)){globa.dispatch(false,strUrl,"该二级热点的图纸已经存在！！！");System.out.println("00000000000");} 
		else{
		int i;
		for(i=0;i<num;i++)
		{
		if (au.getFileName(i).length()>0 && !au.isPic(au.getFileExpName(i))){
		globa.dispatch(false, strUrl, "请选择正确的小图格式如.jpg/.png/.gif,操作");  globa.closeCon();}
		}
		if (au.getFileName(i).length()>0 && !au.isXML(au.getFileExpName(i)))
		{globa.dispatch(false, strUrl, "请选择正确的小图格式如.xml,操作");  globa.closeCon();}
		else{
		Ac ac = new Ac(globa,false);
		String where = " where classoneac ='" + classoneac + "'and classtwoac = '"+classtwoac+"' ";
		ac = ac.show(where);
		String ACname = ac.getStrId();
		String strCity = ac.getStrCity();
		String strCountry = ac.getStrCountry();
		String classOneAc = ac.getClassOneAC();
		String classTwoAc = au.getString("classTwoAc");
	    String strFilePath = application.getRealPath("") + "/acpapers/江苏省/" + strCity + "/" + strCountry + "/" + classOneAc +"/" + classTwoAc +"/";
	    strFilePath = strFilePath.replace('#','_');
	    File path = new File(strFilePath);
	    if (!path.exists()) {
	    	path.mkdirs();
	    }
	    Paper obj=new Paper(globa,false);
	    obj.setAcName(ac.getStrId());
	    Vector <String> FileNames = new Vector<String>();
	    Vector <String> FileNames1 = new Vector<String>();
	    String strCadPic = "";
	    String xmlname = new File(au.getFileName(i)).getName();
	    strCadPic =au.saveFile(strFilePath,xmlname.substring(0,(new File(au.getFileName(i)).getName()).lastIndexOf(".")),i);
	    if(obj.add(strFilePath+xmlname))
	    {
	    System.out.println(strFilePath+xmlname);
	    int k=0;
	    for(k = 0;k<num;k++)
	    {
	    if(au.getFileName(k).length()>0)
	    {
	    String s = new File(au.getFileName(k)).getName();
	    if(obj.isImgExt(s,ACname))
	    {
	    FileNames.add(s);
	    }
	    else FileNames1.add(s);
	    }
	    }
	    if(FileNames1.size()>0)
	    {
	    String files ="";
	    for(int h = 0;h < FileNames1.size(); h++)
	    files += FileNames1.get(h);
	    obj.delete(" where acname = '"+ACname+"'");
	    File dd = new File(strFilePath+xmlname);
	    if(!dd.isDirectory())dd.delete();//删除xml文件
	    globa.dispatch(false,strUrl,files+"不是该二级热点的图纸！！！");
	    }
	    else{
	    for(k = 0;k<num;k++)
	    {
	    if(au.getFileName(k).length()>0)
	    {
	    String s = new File(au.getFileName(k)).getName();
	    s=s.substring(0,s.lastIndexOf("."));
	    strCadPic =au.saveFile(strFilePath,s, k);
	    }
	    }
	    for(int n=0;n<FileNames.size();n++){obj.setImgflag(FileNames.get(n),1,obj.getAcName());}
	    globa.dispatch(true,strUrl);
	   }
	   }
	    else {globa.dispatch(false,strUrl);}
	   }
	   
	}
	}
	else 
	{
	if(action.equals(Constants.UPDATE_STR))
	{   
	    String strUrl="paper_import.jsp";
	    String strUrl1="paper_update.jsp";
	    String strUrl2="paper_list.jsp";
		String classoneac = au.getString("strHotSpot");
		String classtwoac = au.getString("classTwoAc");
		int num = Integer.parseInt(au.getString("papernum"));
		System.out.println("00000000000000");
		int i;
		for(i=0;i<num;i++)
		{
		if (au.getFileName(i).length()>0 && !au.isPic(au.getFileExpName(i)))
		{globa.dispatch(false, strUrl1, "请选择正确的小图格式如.jpg/.png/.gif,操作");  globa.closeCon();}
		}
		if (au.getFileName(i).length()>0 && !au.isXML(au.getFileExpName(i)))
		{globa.dispatch(false, strUrl1, "请选择正确的小图格式如.xml,操作"); globa.closeCon();}
		else{
		
		Ac ac = new Ac(globa,false);
		String where = " where classoneac ='" + classoneac + "'and classtwoac = '"+classtwoac+"' ";
		ac = ac.show(where);
		String ACname = ac.getStrId();
		String strHotSpotId = ac.getStrId();
		String strCity = ac.getStrCity();
		String strCountry = ac.getStrCountry();
		String classOneAc = ac.getClassOneAC();
		String classTwoAc = au.getString("classTwoAc");
	   
		Paper obj=new Paper(globa,false);
	    obj.setAcName(ac.getStrId());
	    Vector <String> FileNames = new Vector<String>();
	     Vector <String> FileNames1 = new Vector<String>();
	    String strFilePath = application.getRealPath("") + "/acpapers/江苏省/" + strCity + "/" + strCountry + "/" + classOneAc +"/" + classTwoAc +"/";
	    //strFilePath = changSpecStr(strFilePath); 
	    strFilePath = strFilePath.replace('#','_');
	    String strFilePath1 = application.getRealPath("") + "/acpapers/江苏省/" + strCity + "/" + strCountry + "/" + classOneAc +"/" + classTwoAc +"1/";
	    strFilePath1 = strFilePath1.replace('#','_');
	     if(!(au.getFileName(i).length()>0))
		{
		//System.out.println("000000000000000"+au.getFileName(i));
		File path = new File(strFilePath);
	    if (!path.exists()) {
	    	path.mkdirs();
	    }
	    String strCadPic = "";
	    int k=0;
	    for(k = 0;k<num;k++)
	    {
	    if(au.getFileName(k).length()>0)
	    {
	    String s = new File(au.getFileName(k)).getName();
	    if(obj.isImgExt(s,ACname))
	    {
	    FileNames.add(s);
	    }
	    else FileNames1.add(s);
	    }
	    }
	    if(FileNames1.size()>0)
	    {
	    String files ="";
	    for(int h = 0;h < FileNames1.size(); h++)
	    files += FileNames1.get(h);
	    globa.dispatch(false,strUrl,files+"不是该二级热点的图纸！！！");
	    }
	    else{
	    for(k = 0;k<num;k++)
	    {
	    if(au.getFileName(k).length()>0)
	    {
	    String s = new File(au.getFileName(k)).getName();
	    s=s.substring(0,s.lastIndexOf("."));
	    strCadPic =au.saveFile(strFilePath,s, k);
	    }
	    }
	    for(int n=0;n<FileNames.size();n++){obj.setImgflag(FileNames.get(n),1,obj.getAcName());}
	    globa.dispatch(true,strUrl2);
	   }
	    }
	    else {
	    File path1 = new File(strFilePath1);
	    if (!path1.exists()) {
	    	path1.mkdirs();
	    }
	     File f = new File(strFilePath);
	    		if(f.isDirectory())
    			{
    			String ff[] = f.list();
    			for(int j=0;j<ff.length;j++)
    			{
    			File delfile = new File(strFilePath +  ff[j]); 
    			File move = new File(strFilePath1+ ff[j]);//将原来的图纸移动到临时文件夹内
    			delfile.renameTo(move);
               delfile.delete();
              
    		}
    		}
    	String strCadPic = "";
	    int k=0;
	    for(k = 0;k<num;k++)
	    {
	    if(au.getFileName(k).length()>0)
	    {
	    String s = new File(au.getFileName(k)).getName();
	    if(obj.isImgExt(s,ACname))
	    {
	    FileNames.add(s);
	    }
	    else FileNames1.add(s);
	    }
	    }
	     if(FileNames1.size()>0)
	    {
	    String files ="";
	    for(int h = 0;h < FileNames1.size(); h++)
	    files += FileNames1.get(h);
	     f = new File(strFilePath1);
	    		if(f.isDirectory())
    			{
    			String ff[] = f.list();
    			for(int j=0;j<ff.length;j++)
    			{
    			File delfile = new File(strFilePath1 +  ff[j]); 
    			File move = new File(strFilePath+ ff[j]);
    			delfile.renameTo(move);
    			if (!delfile.isDirectory()) delfile.delete(); 
               else if (delfile.isDirectory()) { 
               delfile.delete();
               
               }
    		}
    		f.delete();
    		}
	    globa.dispatch(false,strUrl,files+"不是该二级热点的图纸且XML文件名不可修改");
	    }
	    else{
	    for(k = 0;k<num;k++)
	    {
	    if(au.getFileName(k).length()>0)
	    {
	    String s = new File(au.getFileName(k)).getName();
	    s=s.substring(0,s.lastIndexOf("."));
	    strCadPic =au.saveFile(strFilePath,s, k);
	    }
	    }
	    strCadPic =au.saveFile(strFilePath,new File(au.getFileName(k)).getName().substring(0,(new File(au.getFileName(k)).getName()).lastIndexOf(".")),k);
	    String xmlname = new File(au.getFileName(k)).getName();
	    if(obj.add1(strFilePath+xmlname,strHotSpotId))
	    {
	   
	    f = new File(strFilePath1);
	    		if(f.isDirectory())
    			{
    			String ff[] = f.list();
    			for(int j=0;j<ff.length;j++)
    			{
    			File delfile = new File(strFilePath1 +  ff[j]);
    			delfile.delete();
           }
           f.delete();
    		}
        for(int n=0;n<FileNames.size();n++){obj.setImgflag(FileNames.get(n),1,obj.getAcName());}
	    globa.dispatch(true,strUrl2);}
	    else 
	    {
	    f = new File(strFilePath);
	    		if(f.isDirectory())
    			{
    			String ff[] = f.list();
    			for(int j=0;j<ff.length;j++)
    			{
    			File delfile = new File(strFilePath +  ff[j]);
    			delfile.delete();
           }
    		}
    		f = new File(strFilePath1);
	    		if(f.isDirectory())
    			{
    			String ff[] = f.list();
    			for(int j=0;j<ff.length;j++)
    			{
    			File delfile = new File(strFilePath1 +  ff[j]); 
    			File move = new File(strFilePath+ ff[j]);
    			delfile.renameTo(move);
    			if (!delfile.isDirectory()) delfile.delete(); 
               else if (delfile.isDirectory()) { 
               delfile.delete();
               
               }
    		}
    		f.delete();
    		}
	    globa.dispatch(false,strUrl2);
	    }
	   }
	    
	    }
	   
	   }
	   //globa.dispatch(obj.update(ss,obj.getAcName()),strUrl2); 
	
	}
	//关闭数据库连接对象
    }
    }
    }
    
  globa.closeCon();
    }
   
   catch(Exception e)
   {
   		e.printStackTrace();
   	}
%>